#pragma once
// 封装socket
#include <memory>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "Log.hpp"
// 设计模式---模板方法模式!
using namespace ns_log;
// 并发链接的最大客户端数量
static const int backsize_default = 8;
static const int MAX_SIZE = 1024;
class Socket;
using SockPtr = std::shared_ptr<Socket>;
class Socket
{
public:
    // 创建套接字
    virtual void CreateSocket() = 0;
    // 绑定套接字
    virtual void BindSocket(uint16_t port) = 0;
    // 将套接字设为监听状态
    virtual void ListenSocket(int backsize = backsize_default) = 0;
    // 接收新连接
    virtual int Acceptor() = 0;
    // 发送信息
    virtual ssize_t Send(const std::string &info) = 0;
    //地址复用
    virtual void ReuseAddress() = 0;
    // 接收信息
    virtual ssize_t Recv(std::string *out) = 0;
    // 关闭套接字
    virtual void Close() = 0;

public:
    void CreateServerTcpSocket(uint16_t port)
    {
        CreateSocket();
        BindSocket(port);
        ListenSocket();
    }
    void CreateServerUdpSocket(uint16_t port)
    {
        CreateSocket();
        BindSocket(port);
    }
};
class Tcp_Socket : public Socket
{
private:
    int _sockfd;

public:
    Tcp_Socket(int sockfd = -1) : _sockfd(sockfd)
    {
    }
    virtual void ReuseAddress()override
    {
        int opt = 1;
        ::setsockopt(_sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
    }
    void CreateSocket() override
    {
        _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
        if (_sockfd < 0)
        {
            LOG(FATAL, "create tcpsocket error!\n");
            abort();
        }
    }
    void BindSocket(uint16_t port) override
    {
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_addr.s_addr = INADDR_ANY;
        local.sin_port = ::htons(port);
        if (::bind(_sockfd, (struct sockaddr *)&local, sizeof(local)) < 0)
        {
            LOG(FATAL, "bind tcpsocket error!\n");
            abort();
        }
        LOG(DEBUG, "bind tcpsocket success!\n");
    }
    void ListenSocket(int backsize = backsize_default) override
    {
        if (listen(_sockfd, backsize) < 0)
        {
            LOG(FATAL, "liten tcpsocket error!\n");
            abort();
        }
        LOG(DEBUG, "listen socketfd success!\n");
    }
    int Acceptor() override
    {

        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);
        int clientfd = ::accept(_sockfd, (struct sockaddr *)&peer, &len);
        if (clientfd < 0)
        {
            LOG(ERROR, "accpet error!\n");
        }
        return clientfd;
    }
    // 发送信息
    ssize_t Send(const std::string &info) override
    {
        return ::send(_sockfd, info.c_str(), info.size(), 0);
    }
    // 接收信息--输出型参数
    ssize_t Recv(std::string *out) override
    {
        char buffer[MAX_SIZE];
        ssize_t n = ::recv(_sockfd, buffer, sizeof(buffer), 0);
        if (n > 0)
        {
            buffer[n] = 0;
            *out = buffer;
        }
        return n;
    }
    void Close() override
    {
        if (_sockfd > 0)
            ::close(_sockfd);
    }
    ~Tcp_Socket()
    {
    }
};

class Udp_Socket : public Socket
{
private:
    int _sockfd;

public:
    void CreateSocket() override
    {
        _sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            LOG(FATAL, "create tcpsocket error!\n");
            abort();
        }
    }
    void BindSocket(uint16_t port) override
    {
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;

        local.sin_addr.s_addr = INADDR_ANY;
        local.sin_port = ::htons(port);
        if (!bind(_sockfd, (struct sockaddr *)&local, sizeof(local)))
        {
            LOG(FATAL, "bind tcpsocket error!\n");
            abort();
        }
    }
    void Close() override
    {
        if (_sockfd > 0)
            ::close(_sockfd);
    }
};
